<?php include('simple_html_dom.php'); ?>
<?php

	$url = "http://mashable.com/2014/07/23/haris-suleman-pilot-dies/?utm_cid=mash-com-fb-main-link";
	
	//parse de lay host và giao thuc http cho link cua site, dung cho truong hop src cua anh khong co
	$scheme = parse_url($url,0);
	$host = parse_url($url,1);
	
	// $content = file_get_contents($url);
	
	// //khoi tao DomDocumnet
	// $dom = new DOMDocument();
	// //them @ de pass qua loi doc file html
	// @$dom->loadHTMLFile($url);	
	// $dom->preserveWhiteSpace = false;
	// // $dom->recover = true;
	// // $dom->strictErrorChecking = false;


	// // dung Dom lay tat ca the img trong site
	// $els = $dom->getElementsByTagName('img');


	function dlPage($href) {

	    $curl = curl_init();
	    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
	    curl_setopt($curl, CURLOPT_HEADER, false);
	    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
	    curl_setopt($curl, CURLOPT_URL, $href);
	    curl_setopt($curl, CURLOPT_REFERER, $href);
	    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
	    curl_setopt($curl, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 6.1; en-US) AppleWebKit/533.4 (KHTML, like Gecko) Chrome/5.0.375.125 Safari/533.4");
	    $str = curl_exec($curl);
	    curl_close($curl);

		// Create a DOM object
	    $dom = new simple_html_dom();
	    // Load HTML from a string
	    $dom->load($str);

	    return $dom;
	    }
	//tao Dom de xu ly tung element
	$html = dlPage($url);
	$els = $html->find('img');

	//$max_count la bien dem tong phan tu cua $els
	$max_count = 0;
	foreach ($els as $el) {
		$max_count++;
	}

	//kiem tra neu so phan tu lon hon 15 thi lay 15
	$max_count = $max_count <=30 ? $max_count : 30;

	$i=0 ; //bien dem de break khi vuot muc 30 vong lap

	//duyet qua tung phan tu trong the dom img
	foreach ($els as $el) {
		$imgScr = $el->src;
		//kiem tra thu src cua anh co chua host và http khong
		if(substr($imgScr, -4) != '.gif') {
			if(is_null($hostImg = parse_url($imgScr,1))){
				//xy ly them vao host và http://
				$imgScr = $scheme.'://'.$host.substr($imgScr,1);
			}
			//kiem tra width, height cua image
			list($width,$height)  = @getimagesize($imgScr);
			if($width > 200 && $height > 200){
				//xuat ra the html img cua hinh get duoc
				echo "<img src='{$imgScr}' style= 'margin: 20px;'/>";
			}
		}
		$i++;
		if($i>$max_count){
			break;//vuot qua 30 vong thi thoat ra
		}
	}
?>